package it.stasbranger.myproject.web.rest;

import it.stasbranger.myproject.dao.RequestsDAO;
import it.stasbranger.myproject.dao.SuppliersHasRequestsDAO;

import it.stasbranger.myproject.domain.Requests;
import it.stasbranger.myproject.domain.SuppliersHasRequests;

import it.stasbranger.myproject.service.RequestsService;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.WebDataBinder;

import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Spring Rest controller that handles CRUD requests for Requests entities
 * 
 */

@Controller("RequestsRestController")
public class RequestsRestController {

	/**
	 * DAO injected by Spring that manages Requests entities
	 * 
	 */
	@Autowired
	private RequestsDAO requestsDAO;

	/**
	 * DAO injected by Spring that manages SuppliersHasRequests entities
	 * 
	 */
	@Autowired
	private SuppliersHasRequestsDAO suppliersHasRequestsDAO;

	/**
	 * Service injected by Spring that provides CRUD operations for Requests entities
	 * 
	 */
	@Autowired
	private RequestsService requestsService;

	/**
	 * Show all SuppliersHasRequests entities by Requests
	 * 
	 */
	@RequestMapping(value = "/Requests/{requests_idrequests}/suppliersHasRequestses", method = RequestMethod.GET)
	@ResponseBody
	public List<SuppliersHasRequests> getRequestsSuppliersHasRequestses(@PathVariable Integer requests_idrequests) {
		return new java.util.ArrayList<SuppliersHasRequests>(requestsDAO.findRequestsByPrimaryKey(requests_idrequests).getSuppliersHasRequestses());
	}

	/**
	 * Delete an existing SuppliersHasRequests entity
	 * 
	 */
	@RequestMapping(value = "/Requests/{requests_idrequests}/suppliersHasRequestses/{suppliershasrequests_suppliersIdsuppliers}/{suppliershasrequests_requestsIdrequests}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRequestsSuppliersHasRequestses(@PathVariable Integer requests_idrequests, @PathVariable Integer related_suppliershasrequestses_suppliersIdsuppliers, @PathVariable Integer related_suppliershasrequestses_requestsIdrequests) {
		requestsService.deleteRequestsSuppliersHasRequestses(requests_idrequests, related_suppliershasrequestses_suppliersIdsuppliers, related_suppliershasrequestses_requestsIdrequests);
	}

	/**
	 * View an existing SuppliersHasRequests entity
	 * 
	 */
	@RequestMapping(value = "/Requests/{requests_idrequests}/suppliersHasRequestses/{suppliershasrequests_suppliersIdsuppliers}/{suppliershasrequests_requestsIdrequests}", method = RequestMethod.GET)
	@ResponseBody
	public SuppliersHasRequests loadRequestsSuppliersHasRequestses(@PathVariable Integer requests_idrequests, @PathVariable Integer related_suppliershasrequestses_suppliersIdsuppliers, @PathVariable Integer related_suppliershasrequestses_requestsIdrequests) {
		SuppliersHasRequests suppliershasrequests = suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(related_suppliershasrequestses_suppliersIdsuppliers, related_suppliershasrequestses_requestsIdrequests, -1, -1);

		return suppliershasrequests;
	}

	/**
	 * Create a new SuppliersHasRequests entity
	 * 
	 */
	@RequestMapping(value = "/Requests/{requests_idrequests}/suppliersHasRequestses", method = RequestMethod.POST)
	@ResponseBody
	public SuppliersHasRequests newRequestsSuppliersHasRequestses(@PathVariable Integer requests_idrequests, @RequestBody SuppliersHasRequests suppliershasrequests) {
		requestsService.saveRequestsSuppliersHasRequestses(requests_idrequests, suppliershasrequests);
		return suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(suppliershasrequests.getSuppliersIdsuppliers(), suppliershasrequests.getRequestsIdrequests());
	}

	/**
	 * Create a new Requests entity
	 * 
	 */
	@RequestMapping(value = "/Requests", method = RequestMethod.POST)
	@ResponseBody
	public Requests newRequests(@RequestBody Requests requests) {
		requestsService.saveRequests(requests);
		return requestsDAO.findRequestsByPrimaryKey(requests.getIdrequests());
	}

	/**
	 * Delete an existing Requests entity
	 * 
	 */
	@RequestMapping(value = "/Requests/{requests_idrequests}", method = RequestMethod.DELETE)
	@ResponseBody
	public void deleteRequests(@PathVariable Integer requests_idrequests) {
		Requests requests = requestsDAO.findRequestsByPrimaryKey(requests_idrequests);
		requestsService.deleteRequests(requests);
	}

	/**
	 * Register custom, context-specific property editors
	 * 
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder, HttpServletRequest request) { // Register static property editors.
		binder.registerCustomEditor(java.util.Calendar.class, new org.skyway.spring.util.databinding.CustomCalendarEditor());
		binder.registerCustomEditor(byte[].class, new org.springframework.web.multipart.support.ByteArrayMultipartFileEditor());
		binder.registerCustomEditor(boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(false));
		binder.registerCustomEditor(Boolean.class, new org.skyway.spring.util.databinding.EnhancedBooleanEditor(true));
		binder.registerCustomEditor(java.math.BigDecimal.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(java.math.BigDecimal.class, true));
		binder.registerCustomEditor(Integer.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Integer.class, true));
		binder.registerCustomEditor(java.util.Date.class, new org.skyway.spring.util.databinding.CustomDateEditor());
		binder.registerCustomEditor(String.class, new org.skyway.spring.util.databinding.StringEditor());
		binder.registerCustomEditor(Long.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Long.class, true));
		binder.registerCustomEditor(Double.class, new org.skyway.spring.util.databinding.NaNHandlingNumberEditor(Double.class, true));
	}

	/**
	 * Select an existing Requests entity
	 * 
	 */
	@RequestMapping(value = "/Requests/{requests_idrequests}", method = RequestMethod.GET)
	@ResponseBody
	public Requests loadRequests(@PathVariable Integer requests_idrequests) {
		return requestsDAO.findRequestsByPrimaryKey(requests_idrequests);
	}

	/**
	 * Show all Requests entities
	 * 
	 */
	@RequestMapping(value = "/Requests", method = RequestMethod.GET)
	@ResponseBody
	public List<Requests> listRequestss() {
		return new java.util.ArrayList<Requests>(requestsService.loadRequestss());
	}

	/**
	 * Save an existing SuppliersHasRequests entity
	 * 
	 */
	@RequestMapping(value = "/Requests/{requests_idrequests}/suppliersHasRequestses", method = RequestMethod.PUT)
	@ResponseBody
	public SuppliersHasRequests saveRequestsSuppliersHasRequestses(@PathVariable Integer requests_idrequests, @RequestBody SuppliersHasRequests suppliershasrequestses) {
		requestsService.saveRequestsSuppliersHasRequestses(requests_idrequests, suppliershasrequestses);
		return suppliersHasRequestsDAO.findSuppliersHasRequestsByPrimaryKey(suppliershasrequestses.getSuppliersIdsuppliers(), suppliershasrequestses.getRequestsIdrequests());
	}

	/**
	 * Save an existing Requests entity
	 * 
	 */
	@RequestMapping(value = "/Requests", method = RequestMethod.PUT)
	@ResponseBody
	public Requests saveRequests(@RequestBody Requests requests) {
		requestsService.saveRequests(requests);
		return requestsDAO.findRequestsByPrimaryKey(requests.getIdrequests());
	}
}